<?php

function socketData($server, $port, $array) {
    $commonProtocol = getprotobyname ( 'TCP' );
    $socket = socket_create ( AF_INET, SOCK_STREAM, $commonProtocol );
    socket_set_option ( $socket, SOL_SOCKET, SO_RCVTIMEO, array ("sec" => 1, "usec" => 0 ) );
    socket_connect ( $socket, $server, $port );
    $i = 0;
    while ( true ) {
        
        $data = '';
        $data = $array [$i];
        if ($data) {
            echo "\n========================" . $data . "===================\n";
            $data = dealData ( $data );
            socket_write ( $socket, $data, strlen ( $data ) ); //发送数据
        }
        $out = @socket_read ( $socket, 4 );
        if ($out) {
            $out2 = unpack ( "I", $out );
            //print_r($out2);
            $out = @socket_read ( $socket, $out2 [1] );
            echo $out;
            $rs = split ( ' ', $out );
            $method = $rs [0];
            echo "\n===========================================method:" . $method . " Read:" . (strlen ( $out ) + 4) . "Byte===========================================\n";
            unset ( $rs [0] );
            $arr = json_decode ( implode ( ' ', $rs ), true );
            print_r ( $arr );
        }
        $i ++;
    
     //		sleep(10);
    //		usleep(300000);
    }
    socket_close ( $socket );
    return trim ( $out );
}
$data = array (

//'sys_register_name {"name":"keke","pwd":"123456"}',
'sys_login {"name":"keke","pwd":"00"}', 
//'sys_register_sex {"uid":14681,"sex":"0","nickname":"牛x"}',
//'query_house_history {"uid":1013,"type":3,"nums":100,"begin":1,"act":"get","owner":1013,"my_uid":1013}'
//'sys_edit_user_info {"user":{"birthday":"1962-01-0100:00:00","city":"和平","income":0,"height":152,"edu":0,"province":"天津","otherarea":""},"uid":1003}',
//'query_user_info {"uid":1000,"type":0}',
//'query_user_info {"uid":1001,"type":1}',
//'query_user_info {"uid":1002,"type":2}',
//'query_user_info {"my_uid":14605,"type":3,"uid":14605}',
//'query_user_info {"uid":14375,"my_uid":14375,"type":3}',
//'query_user_info {"uid":14375,"my_uid":14375,"type":3}',
//'up_photo {"uid":1016,"type":1}',
//'set_attention {"uid":1024}',


//'delete_attention_friend {"uid":1036,"type":0}',
//'delete_to_black_list {"uid":1002,"type":0}',
//'get_attention {"uid":1001,"col":"my_attt"}',
//'enter_store {"uid":1016}',
//'reload_data {"uid":1001}',
//'get_record {"col":"all","uid":1002}',
//'buy_goods {"owner":1036,"type":1,"goods":[{"id":1036,"nums":1}]}',
//'buy_goods {"owner":1036,"type":1,"goods":[{"id":1001,"nums":5}]}',
//'sell_goods {"goods":[{"id":1142,"rec_id":6}]}',
//'query_goods {"goods_id":[1003]}',
//'query_goods_nums {"goods_id":[1001,1002],"type_1":"clothing", "type_2":"coat", "type_3":"limitcount"}',
//'renew_goods {"goods":[{"id":1128,"rec_id":1,"time":7}]}',
//'query_user_goods_list {"type_1":"clothing","type_2":"coat","type_3":"default","begin":1,"nums":31}',
//'query_user_goods_list {"owner":1010,"type_1":"present","type_2":"all","type_3":"default","begin":1,"nums":31}',
//'query_user_goods_list {"type_1":"furniture","type_2":"sofa","type_3":"all","begin":1,"nums":31}',
//'query_user_goods_list {"nums":18,"type_2":"coat","uid":1002,"type_3":"all","begin":1,"type_1":"clothing"}',
//'enter_store {"uid":1005}',
//'enter_bar {}',
//'ask_talking {"uid":1001,"client_addition":"friend"}',
//'enter_house {"owner":1000}',
//'set_clothing {"goods":[{"id":1024,"rec_id":1}]}',
//'set_furniture {"goods":[{"id":1016,"rec_id":2,"x":38,"y":25,"z":66}]}',
//'set_furniture {"goods":[{"y":262,"z":2,"id":2,"x":100},{"y":269,"z":2,"id":1,"x":414}],"my_uid":1004}'
//'clean_house {"uid":1040}',
//'steal_gold {"uid":1001}',
//'add_album {"album_id":0,"aname":"高效","role":2,"desc":""}',
//'photo_leave_words {"owner":1011,"photo_id":1,"comment_id":0,"comment":1}',
//'delete_album_photo {"type":1,"id":1}',
//'query_album_list {"owner":1008,"begin":1,"nums":5}',
//'query_photo_list {"owner":1002,"album_id":1,"begin":1,"nums":1}',
//'query_photo_words {"owner":1001,"photo_id":1,"begin":2,"nums":1}',
//'send_memo {"owner":1040,"title":"杀了你","content":"是不是针对啊","right":1}',
//'query_house_history {"act":"got","owner":1474,"type":3,"begin":1,"nums":50}', //'query_user_goods_list {"uid":1040,"my_uid":1010,"nums":1001,"type_3":"all","type_2":"all","type_1":"present","begin":1}'
//'query_house_owner_history {"owner":1002}',
//'get_present {"uid":1014,"id":[{"id":1130,"rec_id":2}],"rec_id":35,"my_uid":1014}',
//'set_cover {"album_id":1,"photo_id":2}',
//'set_is_read {"rec_id":1}',
//'del_msg {"rec_id":4}',
//'get_present {"rec_id":19,"id":[{"id":1136,"rec_id":25}],"uid":1013,"my_uid":1013}',
//'up_photo {"uid":1010,"type":1}',
//'send_impression {"uid":1002,"content":"认真"}',
//'get_impression {"uid":1002}'
//'verify_user_photo {"album_id":1,"photo_id":1,"owner":1001,"type":"admit"}'
//'edit_autoinfo {"image":"很酷"}',
//'get_autoinfo {"uid":1439}',
//'get_present {"rec_id":19,"uid":1423,"my_uid":1423,"id":[{"rec_id":15,"id":1131}]}' )//'add_user_data {"uid":1449,"col":"count_invite","add":50}'
//'get_my_task {"uid":1449}',
//'get_my_task {"uid":13960}',
//'add_user_data {"uid":1449,"col":"gold","add":-1000}'
//'game_pk {"uid":1478}',
//'get_game_pk_list {}',
//'get_pker_info {"uid":1451}',
//'save_gold {"add_gold":1000,"hours":4}',
//'get_save_gold_info {}'
//'edit_reg_step {"type":"first_up_head"}'
//'clean_record {"uid":1449,"type":"guest_visit_rec"}',
//'get_house_info {"uid":1449}',
//'sendgoods {"goods":[3318]}'
//'query_house_history {"owner":1450,"act":"get","type":2,"my_uid":1450,"uid":1450,"begin":1,"nums":20}',
//'enter_bar {}',
//'ask_talking {"uid":1449}'，
//'get_day_award {"num":2}'
'talk {"content":"hahahaha","type":"horn"}'
//'do_job {"id":129}'
)
;

socketData ( '192.168.1.238', 8889, $data );

function dealData($data) {
    return pack ( 'I', strlen ( $data ) ) . $data;

}

#2进制（100）
?>
